package cn.com.tool;

import java.sql.*;

/**
 * <p>Project:Academic - Jdbc
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/13 18:31:44
 * @since 1.8
 */
public class Jdbc {
    private static String url = "jdbc:mysql://localhost:3306/hjw1" +
            "?useUnicode=true&characterEncoding=utf8&useSSL=false";
    private static String username = "root";
    private static String password = "123456";

    static {
        // 加载驱动  com.mysql.jdbc.Driver
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("加载驱动失败");
        }
    }

    // 获取连接成功数据库对象
    public static  Connection getConnection(){
        try {
            Connection cn = DriverManager.getConnection(url, username, password);
            return cn;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    //回收连接资源
    public static void close(ResultSet rs, PreparedStatement ps, Connection cn) {
        try {
            if ( rs!=null)  rs.close();
            if ( ps!= null) ps.close();
            if (cn!=null)  cn.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
